using System.IO;
using Newtonsoft.Json;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class CardPanel : MonoBehaviour
{
    private GameObject RootCanvas;
    public GameObject cardPanel;
    public Text cardName;
    public Text cardNums;
    public Text cardContent;
    public Text cardNums2;
    private Button Btn_BaoCun;
    private Button b1;
    private CardScriptableObject cardData;
    private string CardDataStr;
    private string Path;

    private void Awake()
    {
        
    }

    void Start()
    {

        Btn_BaoCun = GameObject.Find("Canvas/CardPanel/Baocun").GetComponent<Button>();
        b1 = GameObject.Find("Canvas/CardPanel/b1").GetComponent<Button>();
        Path = Application.dataPath + "/JsonData/card.json";
        
        if (File.Exists(Path))
        {
            ReadJsonData();
        }
        else
        {
            cardData = ScriptableObject.CreateInstance<CardScriptableObject>();
            cardData.cardName = "红牌";
            cardData.cardType = CardType.Red;
            cardData.content = "触犯违规，出局";
            cardData.nums = 5;
        }
        
        Btn_BaoCun.onClick.AddListener(()=>
        {
            string str= JsonConvert.SerializeObject(cardData);
            File.WriteAllText(Path,str);
        });
        b1.onClick.AddListener(()=>
        {
            ReadJsonData();
        });
        
    }

    private void ReadJsonData()
    {
        string strjson = File.ReadAllText(Path);
        cardData = JsonConvert.DeserializeObject<CardScriptableObject>(strjson);
        Debug.Log("读取数据！");
        cardName.text = cardData.cardName;
        cardNums.text = cardData.nums.ToString();
        cardContent.text = cardData.content;
    }
    
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            cardData.nums++;
            cardNums2.text = cardData.nums.ToString();
        }
    }
}
